// pages/equipment/equipment.js

const util = require('../../utils/util.js');
const api = require('../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    seltag: 1, //1:订单列表 2：设备列表
    orderdata: [],
    equipdata: [],
    userid: '',
    status : 0,
  

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
   
    wx.getStorage({
      key: 'wuserids',
      success: function(res) {
        console.log(res);
        let userid = res.data;
        that.setData({
          userid: userid,
          
        })
        that.getWorderlist();
      },
    })


  },
  gotableinfo: function (e) {
    let that = this;
    var index = e.currentTarget.dataset.index;
    console.log(e);
    wx.navigateTo({
      url: '../orderdetails/orderdetails?orderId=' + that.data.orderdata[index].id
    })
  },
  // 确认订单
  confirmOrder(e) {
    let that = this;
    var index = e.currentTarget.dataset.index;

    let orderdatas = that.data.orderdata;


    util.request(api.Worder, {
      'cont': '2',
      'appointid': orderdatas[index].id,
    }, 'POST').then(function(res) {
      console.log(res);
      if (res.code == 0) {
        //  var order_list = res.data;
        that.setData({
          seltag: 1,
         // status: res.data.status
          //  orderdata: order_list

        })
      } else {
        that.setData({
          seltag: 1,
        })
        util.showErrorToast("数据获取失败" + res.code);
      }
    });


    that.getWorderlist();
  },
  // 取消订单
  cancelOrder(e) {
    let that = this;
    var index = e.currentTarget.dataset.index;

    let orderdatas = that.data.orderdata;


    util.request(api.Worder, {
      'cont': '3',
      ' appointid': orderdatas[index].id,
    }, 'POST').then(function(res) {
      console.log(res);
      if (res.code == 0) {
        //  var order_list = res.data;
        that.setData({
          seltag: 1,
        //  status: res.data.status
          // orderdata: order_list

        })
      } else {
        that.setData({
          seltag: 1,
        })
        util.showErrorToast("数据获取失败" + res.code);
      }
    });

    that.getWorderlist();
  },
  // 获取订单列表
  getWorderlist: function() {
    let that = this;
    util.request(api.Worder, {
      'cont': '1',
      'userid': this.data.userid
    }, 'POST').then(function(res) {
      console.log(res);
      if (res.code == 0) {
        var order_list = res.data;
        that.setData({
          seltag: 1,
          orderdata: order_list,

        })
      } else if (res.code == 400) {
        that.setData({
          seltag: 1,


        })
      } else {
        util.showErrorToast("数据获取失败" + res.code);
      }
    });
  },


  getEquiplist: function() {
    let that = this;
    util.request(api.Machine, {
      'cont': '1',
      'userid': that.data.userid,
    }, 'POST').then(function(res) {
      console.log(res);
      if (res.code == 0) {
        var equip_list = res.data;
        that.setData({
          seltag: 2,
          equipdata: equip_list,

        })
      } else if (res.code == 400) {
        that.setData({
          seltag: 2,
        })
      } else {
        that.setData({
          seltag: 2,
        })
        util.showErrorToast("数据获取失败");
      }
    });
  },

  BlockUp: function(e) {
    var index = e.currentTarget.dataset.index;
    // console.log(index);
    let that = this;
    let equipdatas = that.data.equipdata;
    equipdatas[index].status = 2;
    that.setData({
      equipdata: equipdatas,
    });
    util.request(api.Machine, {
      'cont': '3',
      'machineid': equipdatas[index].id,
    }, 'POST').then(function(res) {
      console.log(res);
      if (res.code == 0) {
        wx.showToast({
          title: '设备已停用',
          icon: 'success',
          duration: 2000,
        })
      } else {
        that.setData({
          seltag: 2,
        })
        util.showErrorToast("数据获取失败" + res.code);
      }
    });


  },
  Enable: function(e) {
    var index = e.currentTarget.dataset.index;
    // console.log(index);
    let that = this;
    let equipdatas = that.data.equipdata;
    equipdatas[index].status = 1;
    that.setData({
      equipdata: equipdatas,
    });
    util.request(api.Machine, {
      'cont': '2',
      'machineid': equipdatas[index].id,
    }, 'POST').then(function(res) {
      console.log(res);
      if (res.code == 0) {
        wx.showToast({
          title: '设备已启用',
          icon: 'success',
          duration: 2000,
        })
      } else {
        util.showErrorToast("数据获取失败" + res.code);
      }
    });




  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})